let ID = ''
let shoucang = false
let dianzan = false



Page({
  data:{
    detail:'',
    shoucangUrl:"../image/shoucang-no.png",
    dianzanUrl:"../image/dianzan-no.png",
    pinglun:[],
    content:''
  },
  onLoad(options){
    ID = options.id
    console.log("详情页的id",ID)
    wx.cloud.database().collection("homelist")
    .doc(ID)
    .get()
    .then(res=>{
      console.log("详情页成功",res)
      shoucang=res.data.shoucang
      dianzan =res.data.dianzan
      this.setData({
        detail:res.data,
        shoucangUrl:shoucang?"../image/shoucang-yes.png":"../image/shoucang-no.png",
        dianzanUrl:dianzan?"../image/dianzan-yes.png":"../image/dianzan-no.png",
        pinglun :res.data.pinglun
      })

    })
    .catch(res=>{
      console.log("详情页失败",res)
    })
  },
   //收藏
   clinkMe(){
    this.setData({
      shoucangUrl:shoucang?"../image/shoucang-no.png":"../image/shoucang-yes.png"
    })
    shoucang=!shoucang
    wx.cloud.callFunction({
      name:"caozuo",
      data:{
        action:1,
        id:ID,
        shoucang:shoucang
      }
    })
  },
  //点赞
  clinkMe2(){
    this.setData({
      dianzanUrl:dianzan?"../image/dianzan-no.png":"../image/dianzan-yes.png"
    })
    dianzan=!dianzan
    wx.cloud.callFunction({
      name:"caozuo",
      data:{
        action:2,
        id:ID,
        dianzan:dianzan
      }
    })
  },
  //获取用户输入的评论内容
  getContent(event){
    this.setData({
      content:event.detail.value
    })
  },
  //发表评论
  fabiao(){
    let content = this.data.content
    if(content.length<4){
      wx.showToast({
        icon:"none",
        title: '评论太短了',
      })
      return 
    }
    let pinglunItem={}
    pinglunItem.name='zyh'
    pinglunItem.content =content
    let pinglunArr= this.data.pinglun
    pinglunArr.push(pinglunItem)

    wx.showLoading({
      title:"发表中。。。。。"
    })

    wx.cloud.callFunction({
      name:"caozuo",
      data:{
        action:3,
        id:ID,
        pinglun:pinglunArr
      }
    }).then(res=>{
      console.log("发表成功",res)
      this.setData({
        pinglun:pinglunArr,
        content:''
      })
      wx.hideLoading()
    }).catch(res=>{
      console.log("发表失败",res)
      wx.hideLoading()
    })



  }




})